Page({
  data: {
    sellRecords: []
  },

  onLoad: function (options) {
    console.log('卖车记录页面加载');
    this.loadSellRecords();
  },

  onShow: function () {
    console.log('卖车记录页面显示');
  },

  onHide: function () {
    console.log('卖车记录页面隐藏');
  },

  onUnload: function () {
    console.log('卖车记录页面卸载');
  },

  // 加载卖车记录数据
  loadSellRecords: function() {
    wx.showLoading({
      title: '加载中...',
    });

    // 模拟API调用
    setTimeout(() => {
      const mockRecords = [
        {
          id: 'sell_001',
          carName: '特斯拉-Model 3 2022款后轮驱动版',
          carImage: 'https://img2.baidu.com/it/u=2344334409,3814459730&fm=26&fmt=auto&gp=0.jpg',
          submitTime: '2022-08-03 13:57:56',
          status: 'quoted', // quoted: 已报价, pending: 待报价, expired: 已过期
          statusText: '已报价',
          price: '26.18万',
          quotedPrice: '24.5万',
          isHot: true,
          description: '车况良好，一手车主，定期保养',
          mileage: '1.27万公里',
          year: '2022年'
        },
        {
          id: 'sell_002', 
          carName: '特斯拉-Model 3 2022款后轮驱动版',
          carImage: 'https://img2.baidu.com/it/u=2344334409,3814459730&fm=26&fmt=auto&gp=0.jpg',
          submitTime: '2022-08-03 13:57:56',
          status: 'pending',
          statusText: '待报价',
          price: '待报价',
          quotedPrice: null,
          isHot: false,
          description: '车况良好，急售',
          mileage: '1.27万公里',
          year: '2022年'
        },
        {
          id: 'sell_003',
          carName: '蔚来-ES6 2021款运动版',
          carImage: 'https://img1.baidu.com/it/u=3523947962,2644095658&fm=26&fmt=auto&gp=0.jpg',
          submitTime: '2022-08-01 10:30:22',
          status: 'expired',
          statusText: '已过期',
          price: '35.8万',
          quotedPrice: '32.0万',
          isHot: false,
          description: '电动SUV，配置丰富',
          mileage: '2.5万公里',
          year: '2021年'
        }
      ];

      this.setData({
        sellRecords: mockRecords
      });

      wx.hideLoading();
    }, 1000);
  },

  // 返回上一页
  goBack: function() {
    const pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.switchTab({
        url: '/pages/mine/mine'
      });
    } else {
      wx.navigateBack({
        delta: 1,
        success: () => {
          console.log('返回成功');
        },
        fail: () => {
          wx.switchTab({
            url: '/pages/mine/mine'
          });
        }
      });
    }
  },

  // 查看报价详情
  viewQuoteDetail: function(e) {
    const recordId = e.currentTarget.dataset.recordId;
    const record = this.data.sellRecords.find(r => r.id === recordId);
    
    if (record.status === 'quoted') {
      wx.showModal({
        title: '报价详情',
        content: `车辆: ${record.carName}\n您的期望价格: ${record.price}\n我们的报价: ${record.quotedPrice}\n\n我们会根据车况进一步调整价格`,
        confirmText: '联系客服',
        cancelText: '知道了',
        success: (res) => {
          if (res.confirm) {
            this.contactService(recordId);
          }
        }
      });
    } else if (record.status === 'pending') {
      wx.showToast({
        title: '评估师正在评估中，请耐心等待',
        icon: 'none',
        duration: 2000
      });
    } else {
      wx.showModal({
        title: '记录已过期',
        content: '此卖车记录已过期，您可以重新提交卖车申请',
        confirmText: '重新提交',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            this.resubmitSell(recordId);
          }
        }
      });
    }
  },

  // 联系客服
  contactService: function(recordId) {
    wx.showActionSheet({
      itemList: ['电话联系', '微信联系', '在线客服'],
      success: (res) => {
        switch(res.tapIndex) {
          case 0:
            wx.makePhoneCall({
              phoneNumber: '400-888-9999'
            });
            break;
          case 1:
            wx.showToast({
              title: '微信号：CarService001',
              icon: 'none',
              duration: 3000
            });
            break;
          case 2:
            wx.showToast({
              title: '正在连接在线客服...',
              icon: 'loading',
              duration: 2000
            });
            break;
        }
      }
    });
  },

  // 重新提交卖车
  resubmitSell: function(recordId) {
    wx.switchTab({
      url: '/pages/sell-car/sell-car'
    });
  },

  // 下拉刷新
  onPullDownRefresh: function() {
    console.log('下拉刷新');
    this.loadSellRecords();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },

  // 删除记录
  deleteRecord: function(e) {
    const recordId = e.currentTarget.dataset.recordId;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条卖车记录吗？',
      confirmText: '删除',
      confirmColor: '#ff4d4f',
      success: (res) => {
        if (res.confirm) {
          const records = this.data.sellRecords.filter(r => r.id !== recordId);
          this.setData({
            sellRecords: records
          });
          
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  }
});
